﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CutShuFa.AutoAction
{
    public class ColorParameterControl : TempUserControl, IParameterControl
    {
        private Panel color_panel;
        private Button button;
        private Parameter _parameter;

        public ColorParameterControl() : base()
        {
            color_panel = new Panel();
            button = new Button();
            InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            panel.SuspendLayout();
            SuspendLayout();

            color_panel.BackColor = SystemColors.MenuHighlight;
            color_panel.BorderStyle = BorderStyle.FixedSingle;
            color_panel.Size = new Size(17, 17);

            button.AutoSize = true;
            button.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            button.Text = "选择颜色";
            button.Click += button_clidk;

            panel.Controls.Add(color_panel);
            panel.Controls.Add(button);

            panel.ResumeLayout(false);
            ResumeLayout(false);
            PerformLayout();
        }

        private void button_clidk(object? sender, EventArgs e)
        {
            ColorDialog colorDialog = new ColorDialog();
            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                color_panel.BackColor = colorDialog.Color;
            }
        }

        public void Init(Parameter parameter)
        {
            _parameter = parameter;
            if (string.IsNullOrEmpty(parameter.Value))
            {
                return;
            }
            this._parameter = parameter;
            color_panel.BackColor = Color.FromArgb(
                     Convert.ToInt32(parameter.Value.Substring(1, 2), 16),
                     Convert.ToInt32(parameter.Value.Substring(3, 2), 16),
                     Convert.ToInt32(parameter.Value.Substring(5, 2), 16)
                 );
            lb_title.Text = parameter.Name;
        }

        public Parameter GetParameter()
        {
            Color c = color_panel.BackColor;
            _parameter.Value = "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
            return _parameter;
        }
    }
}
